<?php defined('SYSPATH') or die('No direct script access.');

/**
 *  微信小程序广告配置
 */
class Controller_Admin_Share_Wxxcx  extends Stourweb_Controller
{
    public function before()
    {
        parent::before();
    }

    public function action_index()
    {

        $action = $this->params['action'];
        if (empty($action)) {
            $models =  DB::select('id','modulename')->from('model')->where("id not in(14,12,4,6,7,11,10,101,109,107) and isopen=1")->execute()->as_array();
            $this->assign('models',$models);
            $this->display('admin/share/wxxcx/index');
        }
        if($action=='update')
        {
            $id = Arr::get($_POST,'id');
            $val = Arr::get($_POST,'val');
            $val =  !empty($val) ?  $val:9999;
            $res = DB::update('xcx_ads')->where("id=$id")->set(array('displayorder'=>$val))->execute();
            if( $res)
            {
                echo 'ok';
            }
            else
            {
                echo 'no';
            }
        }

    }

    /**
     * @function  优惠券指定产品
     */
    public function  action_add_product()
    {
        $action = $this->params['action'];
        if(!$action)
        {
            $models =  DB::select('id','modulename')->from('model')->where("id  in(1,2,5) and isopen=1")->execute()->as_array();
            $this->assign('models',$models);
            $this->display('admin/share/wxxcx/prolist');
        }
        if($action=='read')
        {

            $typeid = Arr::get($_GET,'typeid')?Arr::get($_GET,'typeid'):1;
            $modeldata = DB::select('maintable','modulename')->from('model')->where('id','=',$typeid)->execute()->current();
            $protable = $modeldata['maintable'];
            $start=Arr::get($_GET,'start');
            $limit=Arr::get($_GET,'limit');
            $keyword=Arr::get($_GET,'keyword');
            $w = "a.id is not null and ishidden=0 ";
            if($protable=='model_archive')
            {
                $w .= " and a.typeid=$typeid";
            }
            $w .= empty($keyword)?'':" and a.title like '%{$keyword}%'";
            $totalcount_arr=DB::query(Database::SELECT,"select count(*) as num from sline_$protable a where $w")->execute()->as_array();
            $sql="select a.id,a.title from sline_$protable as a   LEFT JOIN  sline_allorderlist as b   ON (a.id=b.aid and b.typeid=$typeid) where $w    ORDER BY IFNULL(b.displayorder,9999) ASC,a.modtime DESC,a.addtime DESC  limit $start,$limit";
            $list=DB::query(Database::SELECT,$sql)->execute()->as_array();
            foreach($list as &$l)
            {
                $l['typename'] = $modeldata['modulename'];
                $l['bh'] = St_Product::product_series($l['id'], "{$typeid}");//编号
            }
            $result['total']=$totalcount_arr[0]['num'];
            $result['lists']=$list;
            $result['success']=true;
            echo json_encode($result);
        }
        if($action=='del')
        {
            $id = Arr::get($_POST,'id');
            if(is_numeric($id))
            {
                DB::delete('xcx_ads')->where('id','=',$id)->execute();
            }
            echo 'ok';
        }

    }
    // 获取关联的产品
    public function action_pro_list()
    {

        $typeid = Arr::get($_GET,'typeid')?Arr::get($_GET,'typeid'):0;
        $id = Arr::get($_GET,'id');
        $page = Arr::get($_GET,'page');
        $limit = 10;
        $start = ($page-1)*$limit;
        $keyword=Arr::get($_GET,'keyword');
        $w = "a.id is not null";
        $w .= empty($keyword)?'':" and a.protitle like '%{$keyword}%'";
        $w .= empty($typeid)?'':' and a.typeid='.$typeid;
        $totalcount_arr=DB::query(Database::SELECT,"select count(*) as num from sline_xcx_ads a where $w")->execute()->as_array();
        $sql="select a.* from sline_xcx_ads as a where $w  limit $start,$limit";

        $list=DB::query(Database::SELECT,$sql)->execute()->as_array();
        foreach($list as &$l)
        {
            $modeldata = DB::select('modulename')->from('model')->where('id','=',$l['typeid'])->execute()->current();
            $l['typename'] = $modeldata['modulename'];
            $l['bh'] = St_Product::product_series($l['id'], "{$l['typeid']}");//编号
        }
        $result['total']=$totalcount_arr[0]['num'];
        $result['page'] = $page;
        $result['pagesize'] = 10;
        $result['lists']=$list;
        $result['success']=true;
        echo json_encode($result);

    }
    /**
     * @function 保存关联产品信息
     */
    public function action_ajax_prolist_save()
    {

        $subdata = Arr::get($_POST,'subdata');
        $subdata = explode(',',$subdata);
        foreach($subdata as $data)
        {
            $data = explode('_',$data);
            $set_arr=array('proid','typeid','protitle');
            $val_arr=array($data[1],$data[0],$data[2]);
            DB::insert('xcx_ads',$set_arr)->values($val_arr)->execute();
        }
        echo json_encode(array('status'=>'yes'));
    }

    public function action_xcx_setting(){
        $this->display('admin/share/wxxcx/setting');
    }

}